﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PTCapture.Support;
using System.IO;
using PTCapture.PatientInfo;

namespace PTCapture.Forms
{
    public partial class ExerciseManagement : Form
    {
        #region Fields

        /// <summary>
        /// File selection
        /// </summary>
        private OpenFileDialog fileDialog;

        #endregion

        #region Delegates

        /// <summary>
        /// Tie to parent form to assign paths on selection
        /// </summary>
        /// <param name="videoPath"></param>
        /// <param name="imagePath"></param>
        /// <returns></returns>
        public delegate bool customHandler();
        public event customHandler MadeExercise;

        #endregion

        #region Initialization

        /// <summary>
        /// Constructor
        /// </summary>
        public ExerciseManagement()
        {
            InitializeComponent();

            fileDialog = new OpenFileDialog();
            fileDialog.Filter = "Image Files (*.png, *.gif, *jpg, *.jpeg) | *.png;*.gif;*jpg;*.jpeg";
            fileDialog.FilterIndex = 0;
            fileDialog.CheckFileExists = true;
            fileDialog.CheckPathExists = true;
            fileDialog.Multiselect = false;

            lbExercises.SelectedIndexChanged += new EventHandler(lbExercises_SelectedIndexChanged);
            txtDescription.TextChanged += new EventHandler(txtDescription_TextChanged);
            txtName.TextChanged += new EventHandler(txtDescription_TextChanged);
        }

        #endregion

        #region Controls

        /// <summary>
        /// Text changed to save/update.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void txtDescription_TextChanged(object sender, EventArgs e)
        {
            if (txtName.Text.Length > 0 &&
                txtDescription.Text.Length > 0)
            {
                btnCreate.Enabled = true;
            }
            else
            {
                btnCreate.Enabled = false;
            }
        }

        /// <summary>
        /// Exercise selection changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void lbExercises_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbExercises.SelectedIndex > -1)
            {
                btnDelete.Enabled = true;
                btnUpdate.Enabled = true;

                txtName.Text = GlobalVariables.Exercises[lbExercises.SelectedIndex].Name;
                txtDescription.Text = GlobalVariables.Exercises[lbExercises.SelectedIndex].Description;

                if (!string.IsNullOrEmpty(GlobalVariables.Exercises[lbExercises.SelectedIndex].ExampleImageName))
                {
                    lblSelectedImage.Text = GlobalVariables.Exercises[lbExercises.SelectedIndex].ExampleImageName;
                }
                else
                {
                    lblSelectedImage.Text = "[Select Example Image]";
                }
            }
            else
            {
                btnDelete.Enabled = false;
                btnUpdate.Enabled = false;
            }
        }

        /// <summary>
        /// Delete an exercise
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            GlobalVariables.Exercises.RemoveAt(lbExercises.SelectedIndex);
            lbExercises.Items.RemoveAt(lbExercises.SelectedIndex);

            ExerciseSupport.SaveExerciseXML();
            RebindExercises();

            MadeExercise();
        }

        /// <summary>
        /// Update selected
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            GlobalVariables.Exercises[lbExercises.SelectedIndex].Name = txtName.Text;
            GlobalVariables.Exercises[lbExercises.SelectedIndex].Description = txtDescription.Text;

            if (!lblSelectedImage.Text.StartsWith("["))
            {
                GlobalVariables.Exercises[lbExercises.SelectedIndex].ExampleImageName = lblSelectedImage.Text;

                Image i = Image.FromFile(lblSelectedImage.Text);
                GlobalVariables.Exercises.Add(new PatientInfo.Exercise(txtName.Text, txtDescription.Text, lblSelectedImage.Text, Converters.ImageToString(i)));
            }

            ExerciseSupport.SaveExerciseXML();
            RebindExercises();

            MadeExercise();
        }

        /// <summary>
        /// Create a new exercise.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (!lblSelectedImage.Text.StartsWith("["))
            {
                try
                {
                    File.OpenRead(lblSelectedImage.Text);
                }
                catch (Exception)
                {
                    MessageBox.Show("Could not open file!", "Error opening file", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                Image i = Image.FromFile(lblSelectedImage.Text);
                GlobalVariables.Exercises.Add(new PatientInfo.Exercise(txtName.Text, txtDescription.Text, lblSelectedImage.Text, Converters.ImageToString(i)));
            }
            else
            {
                GlobalVariables.Exercises.Add(new PatientInfo.Exercise(txtName.Text, txtDescription.Text, "", ""));
            }

            
            ExerciseSupport.SaveExerciseXML();
            RebindExercises();

            MadeExercise();
        }

        /// <summary>
        /// Select a file.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFileSelector_Click(object sender, EventArgs e)
        {
            if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    lblSelectedImage.Text = fileDialog.FileName;
                }
                catch (Exception)
                {
                    MessageBox.Show("Could not open file!", "Error opening file", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
        }

        #endregion

        #region Support

        /// <summary>
        /// Bind exercises to listbox.
        /// </summary>
        public void RebindExercises()
        {
            // Assign to list box
            lbExercises.Items.Clear();
            for (int i = 0; i < GlobalVariables.Exercises.Count; i++)
            {
                lbExercises.Items.Add(GlobalVariables.Exercises[i].Name);
            }
        }

        #endregion
    }
}
